#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
from flask import request, current_app
from flask.blueprints import Blueprint
from src import json_return, token_required, JSON
from src.backend.api import Backend
from src.utils import util

__author__ = 'lizige'

logger = logging.getLogger(__name__)
bp = Blueprint("index", __name__)


@bp.route("/reservation/list", methods=["GET", "POST"])
@token_required
def get_reservation_list(jsapi):
    """
    患者预约列表
    :param jsapi:
    :return:
    """
    id = jsapi.user_id
    # id = "10310451"
    type = request.values.get("type", 0)
    if not type:
        type = 0
    state = request.values.get("select_state", "0")  # 0：未预约，1：已预约
    status, result = jsapi.reservation_api.POST_AppointRequest_GetRequestInfoListMobile(
        CardType=type, CardNum=id,
        AppointState=state)
    return json_return(status, result)


@bp.route("/res/info", methods=["GET"])
@token_required
def get_res_info(jsapi):
    """
    申请单详情
    :param jsapi:
    :return:
    """
    sheetID = request.values.get("id", "")

    status, result = jsapi.reservation_api.GETPOST_Request_GetById(id=sheetID)
    return json_return(status, result)


@bp.route("/res/date", methods=["GET", "POST"])
@token_required
def get_res_date(jsapi):
    """
    获取预约日期
    :param jsapi:
    :return:
    """
    sheetList = request.values.get("id", "").split(",")
    user_id = jsapi.user_id
    status, result = jsapi.reservation_api.POST_AppointSource_GetAppointSourceDateInfo(SheetIDList=sheetList,
                                                                                       AppointPlaceCode="YDD",
                                                                                       PatientID=user_id)
    return json_return(status, result)


@bp.route("/res/time", methods=["GET", "POST"])
@token_required
def get_res_time(jsapi):
    """
    获取预约时间
    :param jsapi:
    :return:
    """
    sheetList = request.values.get("id", "").split(",")
    date = request.values.get("date", "")
    user_id = jsapi.user_id
    status, result = jsapi.reservation_api.POST_AppointSource_GetAppointSourceTimeInfo(SheetIDList=sheetList,
                                                                                       AppointPlaceCode="YDD",
                                                                                       PatientID=user_id,
                                                                                       AppointDate=date)
    return json_return(status, result)


@bp.route("/res/change", methods=["POST"])
@token_required
def get_res_change(jsapi):
    """
    调整预约时间
    :param jsapi:
    :return:
    """
    sheetList = request.values.get("id", "").split(",")
    sourceID = request.values.get("sourceID", "")
    operatorID = current_app.config.get("OPERATORID")
    operatorName = current_app.config.get("OPERATORNAME")
    status, result = jsapi.reservation_api.POST_AppointSource_UpdateAppointSource(SheetIDList=sheetList,
                                                                                  AppointPlaceCode="YDD",
                                                                                  OperatorID=operatorID,
                                                                                  OperatorName=operatorName,
                                                                                  AppointSourceID=sourceID)
    return json_return(status, result)


@bp.route("/res/hand", methods=["POST"])
@token_required
def get_res_hand(jsapi):
    """
    手动预约
    :param jsapi:
    :return:
    """
    sheetList = request.values.get("id", "").split(",")
    sourceID = request.values.get("sourceID", "")
    user_id = jsapi.user_id
    operatorID = current_app.config.get("OPERATORID")
    operatorName = current_app.config.get("OPERATORNAME")
    status, result = jsapi.reservation_api.POST_AppointSource_AppointSourceManual(SheetIDList=sheetList,
                                                                                  AppointPlaceCode="YDD",
                                                                                  OperatorID=operatorID,
                                                                                  OperatorName=operatorName,
                                                                                  AppointSourceID=sourceID)
    return json_return(status, result)


@bp.route("/res/cancel", methods=["GET", "POST"])
@token_required
def get_reservation_cancel(jsapi):
    """
    取消预约
    :param jsapi:
    :return:
    """
    user_id = jsapi.user_id
    sheetList = request.values.get("sheet_list", "").split(",")
    operatorID = current_app.config.get("OPERATORID")
    operatorName = current_app.config.get("OPERATORNAME")
    status, result = jsapi.reservation_api.POST_AppointSource_CancelSource(
        SheetIDList=sheetList, AppointPlaceCode="YDD",
        OperatorName=operatorName,
        OperatorID=operatorID)
    return json_return(status, result)


@bp.route("/res/onekey", methods=["GET", "POST"])
@token_required
def get_reservation_onekey(jsapi):
    """
    一键预约
    :param jsapi:
    :return:
    """
    user_id = jsapi.user_id
    sheetList = request.values.get("sheet_list", "").split(",")
    operatorID = current_app.config.get("OPERATORID")
    operatorName = current_app.config.get("OPERATORNAME")
    status, result = jsapi.reservation_api.POST_AppointSource_AppointSourceOneKeyClick(
        SheetIDList=sheetList, AppointPlaceCode="YDD", PatientID=user_id,
        OperatorName=operatorName,
        OperatorID=operatorID)
    return json_return(status, result)


@bp.route("/res/hz", methods=["GET", "POST"])
@token_required
def get_reservation_huizhi(jsapi):
    """
    预约回执
    :param jsapi:
    :return:
    """
    id = request.values.get("id", "")

    status, result = jsapi.reservation_api.GETPOST_Request_GetByScheduleId(id=id)
    return json_return(status, result)


@bp.route("/res/careDeatil",methods=["GET","POST"])
@token_required
def get_reservation_care_detail(jsapi):
    """
    回执单注意事项
    :param jsapi:
    :return:
    """
    id = request.values.get("id", "")
    #sheetID = request.values.get("sheetID", "")  # 申请单号
    #type = request.values.get("type", "") #
    logger.warning("初始化webservice服务--回执单注意事项")
    backend = Backend()
    logger.warning("调用RequisitionFind接口--回执单注意事项")

    #status, result = backend.RequisitionFind(SheetID="00020001579", PatientDomainId=4)
    status, result = backend.RequisitionFind(ScheduleID=id, PatientDomainId=8)
    return json_return(status, result)



@bp.route("/baodao/list", methods=["GET", "POST"])
@token_required
def get_baodao_list(jsapi):
    """
    患者报到列表
    :param jsapi:
    :return:
    """
    id = jsapi.user_id
    type = request.values.get("select_type", "1")  # 检查申请单类型
    state = request.values.get("select_state", "1")  # 检查状态

    logger.warning("初始化webservice服务")
    backend = Backend()
    logger.warning("调用RequisitionFind接口")

    status, result = backend.RequisitionFind(PatientId=id, PatientDomainId=0, RequisitionType=type, StateType="5",
                                             StateResult=state)
    # return json_return(200, list)
    return json_return(status, result)


@bp.route("/inpatient/baodao", methods=["GET", "POST"])
@token_required
def get_inpatient_baodao(jsapi):
    """
    报到
    :param jsapi:
    :return:
    """
    sheetID = request.values.get("sheetID", "")
    scheduleID = request.values.get("scheduleID", "")
    type = request.values.get("type")
    # code = "20300000425"
    codetype = "1"  # 1:申请单，2：预约号
    code = sheetID
    if scheduleID:
        code = scheduleID
        codetype = "2"
    status, result = jsapi.app_api.POST_RegAdapter_Regist(code=code, codetype=codetype, requisitiontype=type,
                                                          callerid='001')
    # if status == 200:
    #     # 开始推送提醒接口
    #     params = {
    #         "hospital_id": "086bab30-f874-4b43-949a-be0e713f11b0",
    #         "msg_id": "10",
    #         "msg_name": "检查报到提醒",
    #         "data": [
    #             {
    #                 "push_id": "1ac843bf-5b3d-4523-bfbf-c77fc2345d8e25",
    #                 "patientid": "",
    #                 "openid": "",
    #                 "msg_content": "您好，您已经报到成功，请注意叫号，谢谢！（测试）"
    #
    #             }
    #         ]
    #     }
    #     params['data'][0]['patientid'] = jsapi.user_id
    #     status1, result1 = jsapi.push_api.post(params=JSON.dumps(params))
    #     logger.warning("完成推送：%s:%s", str(status1), str(result1))
    # else:
    #     logger.warning("报到错误原因：%s", str(result))
    return json_return(status, result)


@bp.route("/baodao/queue", methods=["POST"])
@token_required
def post_baodao_queue(jsapi):
    """
    排队信息
    :param jsapi:
    :return:
    """
    scheduleID =request.values.get("scheduleID").split(",")
    # PatientId = jsapi.user_id
    status, result = jsapi.reservation_api.GET_AppointSource_GetQueueInfo(scheduleID=scheduleID)
    
    return json_return(200, result)
